/***************************************************************************
 创建者: hualei
 开始时间: 2018-10-20
 copyright: (C) 华友高科
 修改说明: (每次有修改就添加一条，带有 修改人，修改时间，修改描述)
 example (1) hualei 2018-10-15 类的具体实现代码编写

 ***************************************************************************
 *  MUSIC 音乐指令具体实现
 ***************************************************************************/
#include "musicinstructionlogic.h"
#include "programengine.h"
#include "programset.h"
#include <QMediaPlayer>
#include <QMediaContent>

MusicInstructionLogic::MusicInstructionLogic(int robotIdIn, ProgramSet *programSetIn, ProgramEngine *programEngineIn)
{
    robotId = robotIdIn;
    programSet = programSetIn;
    programEngine = programEngineIn;
    isMusicPlaying = false;
}

E_PROGRAM_LOOP_RESULT MusicInstructionLogic::startMusic(int programNumIn, MusicInstruction instructionIn)
{
    // 使用system 函数调用 madplay 命令播放音乐
    if(instructionIn.instructionType != MUSIC)
    {
        return E_PROGRAM_LOOP_RESULT_ERROR;
    }
    else
    {
        QString musicFileName = QString("%1%2").arg(MUSIC_FILEPATH).arg(instructionIn.musicFileName.remove(QRegExp("\\s")));

        // 终止之前音乐进程
        system("killall -9 madplay &");

        if(instructionIn.musicPlayTimes == 0)
        {
            // 暂停播放
            system("killall -9 madplay &");
            isMusicPlaying = false;

        }
        else if(instructionIn.musicPlayTimes == -1)
        {
            // 循环播放
            currentRuningMusicCommand=(QString("%1%2%3%4")
                                       .arg("madplay -a ")
                                       .arg(QString::number(conversionVolume(instructionIn.musicVolume)))
                                       .arg(" " + musicFileName)
                                       .arg(" -r &"));
            system(currentRuningMusicCommand.toLatin1().data());
            isMusicPlaying = true;
        }
        else
        {
            // 播放一次
            system((QString("%1%2%3%4")
                    .arg("madplay -a ")
                    .arg((QString::number(conversionVolume(instructionIn.musicVolume))))
                    .arg(" " + musicFileName)
                    .arg(" &")).toLatin1().data());
            isMusicPlaying = true;
        }
        std::cout << "The Music File :" << musicFileName.toStdString() << std::endl;
    }
    programEngine->calculateNextLineToRun(programNumIn);

    return programEngine->isTheLastInstruction(programNumIn);
}

int MusicInstructionLogic::playMusic(QString musicNameIn, int musicPlayTimes, int musicVolume)
{
    #ifdef D_USE_ROS
    QString musicFileName = QString("%1%2").arg(MUSIC_FILEPATH).
            arg(musicNameIn);

    // 终止之前音乐进程
    system("killall -9 madplay &");

    if(musicPlayTimes == 0)
    {
        // 暂停播放
        system("killall -9 madplay &");
        isMusicPlaying = false;

    }
    else if(musicPlayTimes == -1)
    {
        // 循环播放
        currentRuningMusicCommand=(QString("%1%2%3%4")
                                   .arg("madplay -a ")
                                   .arg(QString::number(conversionVolume(musicVolume)))
                                   .arg(" " + musicFileName)
                                   .arg(" -r &"));
        system(currentRuningMusicCommand.toLatin1().data());
        isMusicPlaying = true;
    }
    else
    {
        // 播放一次
        system((QString("%1%2%3%4")
                .arg("madplay -a ")
                .arg((QString::number(conversionVolume(musicVolume))))
                .arg(" " + musicFileName)
                .arg(" &")).toLatin1().data());
        isMusicPlaying = true;
    }
    std::cout << "The Music File :" << musicFileName.toStdString() << std::endl;
    #endif
    return 1;

}

void MusicInstructionLogic::stopMusic()
{
    #ifdef D_USE_ROS
//    if(isMusicPlaying)
//    {
        // 终止音乐
        system("killall -9 madplay &");
        isMusicPlaying = false;
        currentRuningMusicCommand.clear();
//    }
#endif
}

int MusicInstructionLogic::playSystemMusic(QString nameIn, int musicPlayTimes)
{
    qDebug()<<"MusicInstructionLogic::playSystemMusic"<<nameIn;
    #ifdef D_USE_ROS
    QString musicFileName = QString("%1%2").arg(SYSTEM_MUSIC_FILEPATH).
            arg(nameIn.remove(QRegExp("\\s")));

    // 终止之前音乐进程
    system("killall -9 madplay &");

    if(musicPlayTimes == 0)
    {
        // 暂停播放
        system("killall -9 madplay &");
        isMusicPlaying = false;

    }
    else if(musicPlayTimes == -1)
    {
        // 循环播放
        currentRuningMusicCommand=(QString("%1%2%3%4")
                                   .arg("madplay -a ")
                                   .arg(QString::number(conversionVolume(100)))
                                   .arg(" " + musicFileName)
                                   .arg(" -r &"));
        system(currentRuningMusicCommand.toLatin1().data());
        isMusicPlaying = true;
    }
    else
    {
        // 播放一次
        system((QString("%1%2%3%4")
                .arg("madplay -a ")
                .arg((QString::number(conversionVolume(100))))
                .arg(" " + musicFileName)
                .arg(" &")).toLatin1().data());
        isMusicPlaying = true;
    }
    std::cout << "The Music File :" << musicFileName.toStdString() << std::endl;
    #endif
    return 1;
}

int MusicInstructionLogic::playBarriorMusic(double barrierRatioIn)
{
    QString musicName;
    if(barrierRatioIn<0.0001)
    {
        musicName="barrierStop.mp3";
    }
    else
    {
        musicName="barrierSlow.mp3";
    }
    #ifdef D_USE_ROS
    QString tmpCommand="killall -9 madplay ;";
    QString musicFileName = QString("%1%2").arg(SYSTEM_MUSIC_FILEPATH).arg(musicName);
    QString musicCommand=QString("%1%2%3%4")
                               .arg("madplay -a ")
                               .arg(QString::number(conversionVolume(90)))
                               .arg(" " + musicFileName)
                               .arg(" -r &");
    tmpCommand+=musicCommand;
    qDebug()<<"MusicInstructionLogic::playBarriorMusic"<<tmpCommand;
    system(tmpCommand.toLatin1().data());
    #endif
    return 1;
}

int MusicInstructionLogic::playErrorMusic()
{
    #ifdef D_USE_ROS
    QString tmpCommand="killall -9 madplay ; ";
    QString musicFileName = QString("%1%2").arg(SYSTEM_MUSIC_FILEPATH).arg("errorMusic.mp3");
    QString musicCommand=QString("%1%2%3%4")
                               .arg("madplay -a ")
                               .arg(QString::number(conversionVolume(90)))
                               .arg(" " + musicFileName)
                               .arg(" -r &");
    tmpCommand+=musicCommand;
    qDebug()<<"MusicInstructionLogic::playErrorMusic"<<tmpCommand;
    system(tmpCommand.toLatin1().data());
    #endif
    return 1;
}

int MusicInstructionLogic::restoreNormalMusic()
{
    #ifdef D_USE_ROS
    QString tmpCommand="killall -9 madplay ; ";
    if(currentRuningMusicCommand.length()>0)
    {
        tmpCommand+=currentRuningMusicCommand;
    }
    qDebug()<<"MusicInstructionLogic::restoreNormalMusic"<<tmpCommand;
    system(tmpCommand.toLatin1().data());
    #endif
    return 1;
}

int MusicInstructionLogic::conversionVolume(int volumeIn)
{
    // 音量转换(-175 ~ +18)取( -60 ~ +0)
//    return (int)((70.0/100.0)*((double)volumeIn)) - 52;//最大１８可能导致无法播放出声音。
     return 60*((double)volumeIn)/100.0-60;
}
